// pages/feedback/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
        id: 0,
        value: "体验问题",
        isActive: true
      },
      {
        id: 1,
        value: "商品，商家投诉",
        isActive: false
      },
    ],
    chooseImgs:[],
    textVal:''
  },
  //外网图片路径
  UpLoadImgs:[],
  handleTabsItemChange(e) {
    const {
      index
    } = e.detail
    let {
      tabs
    } = this.data
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
    this.setData({
      tabs
    })
  },
  handleChooseImg(){
    wx.chooseImage({
      count: 9,
      sizeType: ['original','compressed'],
      sourceType: ['album','camera'],
      success: (result)=>{
        //console.log(result)
        this.setData({
          chooseImgs: [...this.data.chooseImgs,...result.tempFilePaths] 
        })
      },
      fail: ()=>{},
      complete: ()=>{}
    });
  },
  handleRemoveImg(e){
    const {index}=e.currentTarget.dataset
    let chooseImgs=this.data.chooseImgs
    chooseImgs.splice(index,1)
    this.setData({
      chooseImgs
    })
  },
  handleText(e){
    this.setData({
      textVal:e.detail.value
    })
  },
  handleForm(){
    const {textVal,chooseImgs}=this.data
    if (!textVal.trim()) {//不合法
      wx.showToast({
        title: '输入不合法',
        icon: 'none',
        mask: true
      });
      return
    }
    wx.showLoading({
      title: "上传中",
      mask: true,
    });
    if (chooseImgs.length!=0) {
      chooseImgs.forEach((v,i)=>{
        wx.uploadFile({
          url: 'wx-bug-shop.oss-cn-beijing.aliyuncs.com',
          filePath: v,
          name: "flie",
          formData: {
            name: v,
            key: '${filename}',
            policy: 'eyJleHBpcmF0aW9uIjoiMjAyMC0wOC0wMVQxMjowMDowMC4wMDBaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF1dfQ ==',
            OSSAccessKeyId: 'LTAI4FoQAoE3UgbVFV6nS7ke',
            success_action_status: '200',
            signature: 'oDHSMEeGJiW9fIau8q X0QPETrs ='
          },
          success: (result)=>{
            //console.log(result);
            let url=JSON.parse(result.data).url
            this.UpLoadImgs.push(url)
            if (i===chooseImgs.length-1) {//图片处理完毕
              console.log('提交成功');//提交后台
              this.setData({//重置
                textVal:'',
                chooseImgs:[]
              })
              wx.navigateBack({
                delta: 1
              });
            }
          },
          fail: (err)=>{
            console.log(err)
          },
          complete: ()=>{}
        });
      })
    }else{
      wx.hideLoading();
      wx.navigateBack({
        delta: 1
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})